<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;

class CascadeController extends CommonController
{

    private $cascadeDataModel;

    public function __construct()
    {
        parent::__construct();
        $this->cascadeDataModel = D('cascadedata');
        $this->cascadeModel = D('cascade');
    }

    public function index()
    {
        $this->display();
    }

    public function casCade()
    {
        $sort = I('request.sort', 'orderid');
        $order = I('request.order', 'asc');
        $cascade = M('cascade');
        if ($sort && $order) {
            $result = $cascade->order($sort . " " . $order)->select();
        } else {
            $result = $cascade->select();
        }

        $this->ajaxReturn($result);

        exit;
    }


    public function casCadeData()
    {
        $levelData = array();
//        if($data['level'] == 2){
//            js_alert('该层级下已无分类');
//        }
        $sign = I('request.sign');
        $level = I('request.level', 0);
        $value = I('request.value');
        $map['sign'] = $sign;
        $maxLevel = $this->cascadeDataModel->where($map)->max('level');
        $this->assign('level', $level);
        $this->assign('sign', $sign);
        $this->assign('dataValue', $value);
        $this->display();
    }

    public function getCasCadeData()
    {
        $sign = I('request.sign');
        //排序
        $sort = I('request.sort');
        $order = I('request.order');
        if ($sort && $order) {
            $order = $sort . " " . $order;
        } else {
            $order = 'orderid asc';
        }
        
        $level = $_REQUEST['level'] ? $_REQUEST['level'] : "0";
        $value = I('request.datavalue', 0);
        $map['datagroup'] = $sign;
        //判断
        if ($value == 0) {

        } else if ($value % 500 == 0) {
            $map['datavalue'] = array(array('gt', $value + 0), array('lt', $value + 500));
        } else {
            $map['datavalue'] = array('like', $value . ".%%%");
        }
        $map['datagroup'] = $sign;
        $map['level'] = $level;
        $result = $this->cascadeDataModel
            ->where($map)
            ->order($order)
            ->select();
//        echo $this->cascadeDataModel->_sql();die;
        $this->ajaxReturn($result);
        exit;
    }

    public function delCasCade()
    {
        $casId = I('request.id', '');
        echo "暂无权限";
        die;
//        if($casId){
//            $res = M("cascade")->where('id='.$casId)->delete();
//            if($res){
//                $this->display('index');
//            }
//        }
    }

    public function delCasCadeData()
    {
        $casId = I('request.id', '');
        echo "暂无权限";
        die;
//        if($casId){
//            $res = M("cascadedata")->where('id='.$casId)->delete();
//            if($res){
//                $this->display('casCadeData');
//            }
//        }
    }


    public function showCasCade()
    {
        if ($_REQUEST['showWindow'] == 1) {
            $casId = I('request.casId', '');
            if ($casId) {
                $result = M("cascade")->where("id=" . $casId)->find();
                if ($result) {
                    $this->assign('casData', $result);
                }
            }
            if ($_REQUEST['operate'] == 'edit') {
                $data['groupname'] = I('request.groupname');
                $data['groupsign'] = I('request.groupsign');
                $data['orderid'] = I('request.orderid');
                $res = M('cascade')->where("id=" . $casId)->save($data);
                if ($res) {
                    $return['status'] = 1;
                    $return['msg'] = "修改成功";
                } else {
                    $return['status'] = 0;
                    $return['msg'] = "修改失败";
                }
                $this->ajaxReturn($return);
                exit;
            }
            $this->assign('showWindow', 1);
            $this->display("editCasCade");
        }
        if ($_REQUEST['showWindow'] == 2) {
            $casId = I('request.casId', '');
            if ($casId) {
                $result = M("cascadedata")->where("id=" . $casId)->find();
                if ($result) {
                    $this->assign('casData', $result);
                }
            }
            if ($_REQUEST['operate'] == 'edit') {
                $data['dataname'] = I('request.dataname');
                $data['datavalue'] = I('request.datavalue');
                $data['orderid'] = I('request.orderid');
                $res = M('cascadedata')->where("id=" . $casId)->save($data);
                if ($res) {
                    $return['status'] = 1;
                    $return['msg'] = "修改成功";
                } else {
                    $return['status'] = 0;
                    $return['msg'] = "修改失败";
                }
                $this->ajaxReturn($return);
                exit;
            }
            $this->assign('showWindow', 2);
            $this->display("editCasCade");
        }
    }


    //新增修改级联数据cascade表
    public function addCascade()
    {
        $data = array();
            $groupname= I('request.groupname');
            $groupsign= I('request.groupsign');
            $orderid = I('request.orderid');
            $action = I('request.action');
        if ($groupname == "" or $groupsign == "") {
            $this->ajaxReturn(array('status' => 0, 'msg' => '级联数据不能为空'));
        }
        //判断级联
        $isWhere = "groupname = '{$groupname}' or groupsign = '{$groupsign}'";
        if ($action == "update") {
            $id = I('request.id');
            $isWhere =  "(".$isWhere.") and id !=" . $id;
        }
        $row = $this->cascadeModel->where($isWhere)->find();
        if (is_array($row)) {
            if ($row['groupname'] == $groupname) {
                $this->ajaxReturn(array('status' => 0, 'msg' => '级联名称已存在'));
            }
            if ($row['groupsign'] == $groupsign) {
                $this->ajaxReturn(array('status' => 0, 'msg' => '级联标识已存在'));
            }
        }
        //组成数组
        $data['groupname'] = $groupname;
        $data['groupsign'] = $groupsign;
        $data['orderid'] = $orderid;
        if($action == "update"){
            $map['id'] = $id;
            $res = $this->cascadeModel->where($map)->save($data);
        }else{
            $res = $this->cascadeModel->add($data);
        }
        if($res){
            $this->ajaxReturn(array('success' => 1, 'msg' => '数据更新成功','id'=>$id));
        }else{
            $this->ajaxReturn(array('success' => 0, 'msg' => '数据更新失败'));
        }



    }


    //新增修改级联数据cascadedata表
    public function addCascadeData()
    {
        //传值
        $formDataStr = $_POST['jsonCasData'];
        parse_str($formDataStr, $formdata); //转移url为数组
        $cascade = M('cascadedata');
        $data = array();
        $data['dataname'] = $formdata['dataname'];
        $data['datavalue'] = $formdata['datavalue'];
        $data['orderid'] = $formdata['orderid'];
        if ($data['dataname'] == "" or $data['datavalue'] == "") {
            $this->ajaxReturn(array('status' => 0, 'msg' => '级联数据值不能为空'));
        }
        $data['level'] = I('request.level');
        $data['datagroup'] = I('request.sign', '');
        //新增级联数据
        if ($formdata['action'] == "add") {
            $isWhere = "(dataname = '{$data['dataname']}' or datavalue = '{$data['datavalue']}') and datagroup = '{$data['datagroup'] }'";
            $row = $cascade->where($isWhere)->find();
            if (is_array($row)) {
                if ($row['dataname'] == $data['dataname']) {
                    $this->ajaxReturn(array('status' => 0, 'msg' => '级联数据名称已存在'));
                }
                if ($row['datavalue'] == $data['datavalue']) {
                    $this->ajaxReturn(array('status' => 0, 'msg' => '级联数据值已存在'));
                }
            }
            $res = $cascade->add($data);
            if ($res) {
                $this->ajaxReturn(array('status' => 1, 'msg' => '数据新增成功'));
            } else {
                $this->ajaxReturn(array('status' => 0, 'msg' => '数据新增失败'));
            }
        }
        //修改级联数据
        if ($formdata['action'] == "update") {
            $level = $cascade->where("id=" . $formdata['id'])->getField('level');
            $data['level'] = $level;
            $res = $cascade->where("id=" . $formdata['id'])->save($data);
            if ($res) {
                $this->ajaxReturn(array('status' => 1, 'msg' => '数据修改成功'));
            } else {
                $this->ajaxReturn(array('status' => 0, 'msg' => '数据修改失败'));
            }
        }

    }
}